//
//  RegistViewController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/1.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "RegistViewController.h"
#import "LMCountDownButton.h"
#import "UserAgreementViewController.h"
@interface RegistViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *inviteCodeTextField;
@property (weak, nonatomic) IBOutlet LMCountDownButton *codeButton;
@property (weak, nonatomic) IBOutlet UIButton *agreeButton;

@end

@implementation RegistViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    LMCountDownButton *sendCodeButton = [[LMCountDownButton alloc] init];
    [sendCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [sendCodeButton setTitleColor:[UIColor colorWithHexString:@"#FF7321"] forState:(UIControlStateNormal)];
    sendCodeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    sendCodeButton.frame = self.codeButton.bounds;
    [sendCodeButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.codeButton addSubview:sendCodeButton];
}
// 获取验证码
-(void)click:(LMCountDownButton *)sender {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //去除字符串中空格
    self.phoneTextField.text = [self.phoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![self isMobileNumberOnly:self.phoneTextField.text]) {
        ShowToastWithText(@"请输入正确电话号码");
        return;
    }
    NSDictionary *dic = @{@"phone":self.phoneTextField.text};
    
    [NetWorkTool POST:sendSms param:[dic mutableCopy] success:^(id dic) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [sender startCountDown];
        ShowToastWithText(@"验证码发送成功");
    } other:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:NO];
}
// 立即注册
- (IBAction)registButtonClick:(UIButton *)sender {
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"phone"] = self.phoneTextField.text;
    param[@"code"] = self.codeTextField.text;
    param[@"parentCode"] = self.inviteCodeTextField.text;
    //去除字符串中空格
    self.phoneTextField.text = [self.phoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![self isMobileNumberOnly:self.phoneTextField.text]) {
        ShowToastWithText(@"请输入正确的手机号");
        return;
    }
    
    if ([self.codeTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"验证码不能为空");
        return;
    }
    if ([self.inviteCodeTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"邀请码不能为空");
        return;
    }
    if (self.agreeButton.selected == NO) {
        ShowToastWithText(@"请阅读并同意用户协议及隐私政策");
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:userRegister param:param success:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    } other:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:NO];
}
// 我已同意
- (IBAction)agreeButtonClick:(UIButton *)sender {
    self.agreeButton.selected = !self.agreeButton.selected;
}
// 用户协议
- (IBAction)userAgreementButtonClick:(UIButton *)sender {
    UserAgreementViewController * vc = [[UserAgreementViewController alloc] init];
    vc.urlString = [NSString stringWithFormat:@"%@%@",HOST_LINK,UserProtocal];
    vc.titleString = @"用户协议";
    [self.navigationController pushViewController:vc animated:YES];
}
// 隐私政策
- (IBAction)privacyPolicyButtonClick:(UIButton *)sender {
    UserAgreementViewController * vc = [[UserAgreementViewController alloc] init];
    vc.urlString = [NSString stringWithFormat:@"%@%@",HOST_LINK,UserPolicy];
    vc.titleString = @"隐私政策";
    [self.navigationController pushViewController:vc animated:YES];
}
// 已有账号
- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

///判断是否为电话号码
- (BOOL)isMobileNumberOnly:(NSString *)mobileNum {
    NSString * MOBILE = @"^((10[0-9])|(11[0-9])|(12[0-9])|(13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    if ([regextestmobile evaluateWithObject:mobileNum] == YES) {
        return YES;
    } else {
        return NO;
    }
}

@end
